<?php

namespace app\foreigndata\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Buffbuy extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $appid = '1086921698142103';
    protected $pubKey = 'DFXTnfDf*?SxTF0zuqW4VW15k9bifiaV*rVzYDI5hh8TQvbkQbabBRz?uCM01YOJ';
    protected $url = "https://trade.api.52buff.com";

    //52buff购买金币
    public function index(){
        $num = $this->request->get("num");
        $num = 1000;
        $areaArr = array_flip($this->getArea());
        $areaId_test = array_values($areaArr);
        $areaId = $areaId_test[4];
        $sign = md5(md5("amount".$num."appId1086921698142103areaId".$areaId."isAutoTrade1").$this->pubKey);

        $urls = $this->url."/CP/sell/queryGoodsInfo?appId=".$this->appid."&areaId=".$areaId."&amount=".$num."&isAutoTrade=1"."&sign=".$sign;
        $return = $this->GetHttp($urls);
        dump($return);
    }

    public function buyGoods(){
        $num = '100';
        $areaArr = array_flip($this->getArea());
        $areaId_test = array_values($areaArr);
        $price = '1.00';
        $urls = $this->url."/CP/sell/buyGold";

        $areaId = $areaId_test[0];
        $sign = md5(md5("amount".$num."appId1086921698142103areaId".$areaId."cpOrderId123isAutoTrade0price".$price.'receiverabc').$this->pubKey);
        $data = array(
            'appId'=>'1086921698142103',
            'areaId'=>$areaId,
            'isAutoTrade'=>0,
            'cpOrderId'=>'123',
            'price'=>$price,
            'receiver'=>'abc',
            'amount'=>$num,
            'sign'=>$sign
        );
        $return = $this->curl_post($urls,$data);
        return $return;


    }

    public function getMoney(){
        $sign = md5(md5("appId1086921698142103").$this->pubKey);
        $urls = $this->url."/CP/sell/getBalance"."?appId=".$this->appid."&sign=".$sign;
//        $urls = $this->url."/CP/sell/getBalance";
//        $data = array(
//            'appId'=>$this->appid,
//            'privateKey'=>$this->pubKey
//        );
        $return = $this->GetHttp($urls);
        dump($return);die;
    }

    public function curl_post($url , $data=array()){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        // POST数据

        curl_setopt($ch, CURLOPT_POST, 1);

        // 把post的变量加上

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);

        curl_close($ch);
        var_dump($output);die;

    }


    /**
     * get
     * @param string $url 请求地址
     */
    function GetHttp($url)
    {
        // 关闭句柄
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 0);   //只需要设置一个秒的数量就可以
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl); //返回api的json对象
        if (curl_exec($curl) === false) {
            return 'Curl error: ' . curl_error($curl);
        }
        //关闭URL请求
        curl_close($curl);
        return json_decode($tmpInfo); //返回json对象
    }

    public function getArea(){
        return array(
              10000=>"西南1区",
              10001=> "西南2区",
              10002=> "西南3区",
              10003=> "江苏1区",
              10004=> "江苏2区",
              10005=> "江苏3区",
              10006=> "江苏4区",
              10007=> "江苏5/7区",
              10008=> "江苏6区",
              10009=> "江苏8区",
              10010=> "广东1区",
              10011=> "广东2区",
              10012=> "广东6区",
              10013=> "广东3区",
              10014=> "广州1/2区",
              10015=> "广东4区",
              10016=> "广东5区",
              10017=> "广东7区",
              10018=> "广东8区",
              10019=> "广东9区",
              10020=> "广东10区",
              10021=> "广东11区",
              10022=> "广东12区",
              10023=> "广东13区",
              10024=> "福建1区",
              10025=> "福建2区",
              10026=> "福建3/4区",
              10027=> "浙江1区",
              10028=> "浙江2区",
              10029=> "浙江3区",
              10030=> "浙江4/5区",
              10031=> "浙江6区",
              10032=>"浙江7区",
              10033=> "上海1区",
              10034=> "上海2区",
              10035=> "上海3区",
              10036=> "上海4/5区",
              10037=> "江西1区",
              10038=> "江西2区",
              10039=> "江西3区",
              10040=> "陕西1区",
              10041=> "陕西2/3区",
              10042=> "重庆1区",
              10043=> "重庆2区",
              10044=> "西北2/3区",
              10045=> "西北1区",
              10046=> "安徽2区",
              10047=> "安徽3区",
              10048=> "安徽1区",
              10049=> "新疆1区",
              10050=> "华北1区",
              10051=> "华北2区",
              10052=> "华北3区",
              10053=> "华北4区",
              10054=> "河南1区",
              10055=> "河南2区",
              10056=> "河南3区",
              10057=> "河南4区",
              10058=> "河南5区",
              10059=> "河南6区",
              10060=> "河南7区",
              10061=> "辽宁1区",
              10062=> "辽宁3区",
              10063=> "辽宁2区",
              10064=> "山西1区",
              10065=> "山西2区",
              10066=> "黑龙江1区",
              10067=> "黑龙江2/3区",
              10068=> "吉林1/2区",
              10069=> "山东6区",
              10070=> "山东1区",
              10071=> "山东2/7区",
              10072=> "山东3区",
              10073=> "山东4区",
              10074=> "山东5区",
              10075=> "河北4区",
              10076=> "河北5区",
              10077=> "河北2/3区",
              10078=> "河北1区",
              10079=> "天津1区",
              10080=> "内蒙古1区",
              10081=> "四川1区",
              10082=> "四川2区",
              10083=> "四川3区",
              10084=> "四川4区",
              10085=> "四川5区",
              10086=> "四川6区",
              10087=> "湖南1区",
              10088=> "湖南2区",
              10089=> "湖南3区",
              10090=> "湖南4区",
              10091=> "湖南5区",
              10092=> "湖南6区",
              10093=> "湖南7区",
              10094=> "贵州1区",
              10095=> "云南1区",
              10096=> "云贵1区",
              10097=> "湖北1区",
              10098=> "湖北2区",
              10099=> "湖北7区",
              10100=> "湖北8区",
              10101=> "湖北3区",
              10102=> "湖北4区",
              10103=> "湖北5区",
              10104=> "湖北6区",
              10105=> "广西1区",
              10106=> "广西2/4区",
              10107=> "广西3区",
              10108=> "广西5区",
              10109=> "东北1区",
              10110=> "东北2区",
              10111=> "东北3/7区",
              10112=> "东北4/5/6区",
              10113=> "北京1区",
              10114=> "北京2/4区",
              10115=> "北京3区"
        );
    }


}
